﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace MineLaunch.Setting
{
    public class SettingsBase
    {
        private string SettingsDirectory;

        protected ModelSettingsBase LoadSettings(string filepath)
        {
            object settings = null;
            if (!File.Exists(filepath))
            {
                return null;
            }
            try
            {
                using (FileStream filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    settings = formatter.Deserialize(filestream);
                }
            }
            catch (Exception) { return null; }
            return settings as ModelSettingsBase;
        }

        protected void SaveSettings(string filepath, ModelSettingsBase settings)
        {
            using (FileStream filestream = new FileStream(filepath, FileMode.Create, FileAccess.Write))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(filestream, settings);
            }
        }
    }
}
